package com.peng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.peng.entity.User;
import com.peng.dao.UserMapper;
import com.peng.service.FileService;
import com.peng.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peng.utils.SystemConstants;
import com.peng.vo.query.UserQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private FileService fileService;

    /**
     * 根据用户名查询用户信息
     *
     * @param username
     */
    @Override
    public User findUserByUserName(String username) {
        // 创建条件构造器对象
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        // 执行查询
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 分页查询用户信息
     *
     * @param page
     * @param userQueryVo
     * @return
     */
    @Override
    public IPage<User> findUserListByPage(IPage<User> page, UserQueryVo userQueryVo) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        //部门编号
        queryWrapper.eq(!ObjectUtils.isEmpty(userQueryVo.getDepartmentId()), User::getDepartmentId, userQueryVo.getDepartmentId())
                // 用户名
                .like(!ObjectUtils.isEmpty(userQueryVo.getUsername()), User::getUsername, userQueryVo.getUsername())
                // 真实姓名
                .like(!ObjectUtils.isEmpty(userQueryVo.getRealName()), User::getRealName, userQueryVo.getRealName())
                // 电话号码
                .like(!ObjectUtils.isEmpty(userQueryVo.getPhone()), User::getPhone, userQueryVo.getPhone());

        return baseMapper.selectPage(page, queryWrapper);
    }

    /**
     * 删除用户信息
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean deleteById(Long id) {
        // 查询
        User user = baseMapper.selectById(id);
        // 删除用户角色关系
        baseMapper.deleteUserRole(id);
        // 删除用户
        if (baseMapper.deleteById(id) > 0) {
            // 判断用户是否存在
            if (user != null && !ObjectUtils.isEmpty(user.getAvatar()) && !user.getAvatar().equals(SystemConstants.DEFAULT_AVATAR)) {
                // 删除文件
                fileService.deleteFile(user.getAvatar());
            }
            return true;
        }
        return false;
    }

    /**
     * 分配角色
     *
     * @param userId
     * @param roleIds
     * @return
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean saveUserRole(Long userId, List<Long> roleIds) {
        // 删除该用户对应的角色信息
        baseMapper.deleteById(userId);
        // 保存用户角色信息
        int count = baseMapper.saveUserRole(userId, roleIds);
        return count > 0;
    }
}
